package org.lex.jadfront.core;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.lex.io.utils.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Jad {
	private static final String JAD = "C:\\_liuex\\apps\\jad\\jad.exe";

	private Logger log = LoggerFactory.getLogger(Jad.class);
	private Process jad = null;

	public void decompile(File inputClassFile, File outputDirectory, JadOptions opt) throws IOException {
		jad = this.createProcess(inputClassFile, outputDirectory, opt);
		ProcessReader out = new ProcessReader(jad.getInputStream(), System.out);
		ProcessReader err = new ProcessReader(jad.getErrorStream(), System.err);
		ThreadPool.submit(out);
		ThreadPool.submit(err);
	}

	private Process createProcess(File inputClassFile, File outputDirectory, JadOptions opt) throws IOException {
		FileUtils.createFolder(outputDirectory);
		log.info("in=" + inputClassFile + ", out=" + outputDirectory);
		List<String> cmds = new ArrayList<String>();

		cmds.add(JAD);
		cmds.add("-s");
		cmds.add("java");

		cmds.add("-d");
		cmds.add("\"" + outputDirectory.getAbsolutePath() + "\"");

		cmds.add("\"" + inputClassFile.getAbsolutePath() + "\"");
		System.out.println("args: " + cmds);

		ProcessBuilder pb = new ProcessBuilder(cmds);
		return pb.start();
	}
}
